package com.edu.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * @author Administrator
 *	BaseDao 主要完成 获取数据库连接 和 关闭资源这两个操作
 *
 */
public class BaseDao {
	
	private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
	
	private static final String USERNAME = "root";
	
	private static final String PASSWORD = "123456";
	
	private static final String URL = "jdbc:mysql://localhost:3306/vehicleManagement";
	
	//加载驱动 只执行一次
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动失败");
			e.printStackTrace();
		}
	}
	
	//获取数据库连接
	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(URL,USERNAME, PASSWORD);
		} catch (SQLException e) {
			System.out.println("获取连接失败");
			e.printStackTrace();
		}
		return con;
	}
	
	
	//关闭资源
	public static void closeAll(Connection con,Statement stmt,ResultSet res) {
		if(res != null) {
			try {
				res.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
